package com.mvd.app.activity;

import android.app.AlertDialog;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.kongzue.dialog.interfaces.OnMenuItemClickListener;
import com.kongzue.dialog.util.TextInfo;
import com.kongzue.dialog.v3.BottomMenu;
import com.kongzue.dialog.v3.MessageDialog;
import com.kongzue.dialog.v3.TipDialog;
import com.kongzue.dialog.v3.WaitDialog;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;
import com.mvd.app.R;
import com.mvd.app.application.BaseApplication;
import com.mvd.app.commons.CommonUtils;
import com.mvd.app.commons.DateUtil;
import com.mvd.app.commons.JsonUtil;
import com.mvd.app.commons.PDALogUtils;
import com.mvd.app.commons.SharedPreferencesUtils;
import com.mvd.app.commons.UtilsLog;
import com.mvd.app.model.CarTypeModel;
import com.mvd.app.model.QueryModel;
import com.mvd.app.model.SearchPlatformCarModel;
import com.mvd.app.model.TianJin22Model;
import com.uuzuche.lib_zxing.activity.CaptureActivity;
import com.uuzuche.lib_zxing.activity.CodeUtils;
import com.uuzuche.lib_zxing.activity.ZXingLibrary;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.darsh.multipleimageselect.helpers.Constants.REQUEST_CODE;

/**
 * @author Administrator
 * @version V1.0
 * @Title: PreChereckActivity
 * @Package com.mvd.app.activity
 * @Description: 天津预检
 * @date 2022-04-20 10:54
 **/

public class PreChereckActivity extends BaseActivity {
    @BindView(R.id.title_btn_left)
    ImageButton titleBtnLeft;
    @BindView(R.id.title_btn_left_txt)
    Button titleBtnLeftTxt;
    @BindView(R.id.title_txt)
    TextView titleTxt;
    @BindView(R.id.iv_headtip)
    ImageView ivHeadtip;
    @BindView(R.id.title_btn_right_txt)
    Button titleBtnRightTxt;
    @BindView(R.id.title_btn_right)
    ImageButton titleBtnRight;
    @BindView(R.id.ac_prechreck_number)
    EditText acPrechreckNumber;
    @BindView(R.id.ac_prechreck_user)
    TextView acPrechreckUser;
    @BindView(R.id.ac_prechreck_type)
    TextView acPrechreckType;
    @BindView(R.id.ac_prechreck_waijian_submit)
    Button acPrechreckWaijianSubmit;
    @BindView(R.id.ac_prechreck_waijian_query)
    Button acPrechreckWaijianQuery;
    @BindView(R.id.ac_prechreck_vin)
    EditText acPrechreckVin;
    @BindView(R.id.ac_prechreck_category)
    TextView acPrechreckCategory;
    @BindView(R.id.ac_prechreck_vin2)
    TextView acPrechreckVin2;
    @BindView(R.id.ac_prechreck_VehicleStatus)
    TextView acPrechreckVehicleStatus;
    @BindView(R.id.ac_prechreck_CertificateNo)
    TextView acPrechreckCertificateNo;
    @BindView(R.id.ac_prechreck_RegisteDate)
    TextView acPrechreckRegisteDate;
    @BindView(R.id.ac_prechreck_ValidTill)
    TextView acPrechreckValidTill;
    @BindView(R.id.ac_prechreck_ScrapDate)
    TextView acPrechreckScrapDate;
    @BindView(R.id.ac_prechreck_Status)
    TextView acPrechreckStatus;
    @BindView(R.id.ac_prechreck_order)
    Button acPrechreckOrder;
    @BindView(R.id.ac_prechreck_new_yujian)
    Button acPrechreckNewYujian;
    @BindView(R.id.ac_prechreck_qiandao_qiandao)
    Button btn_qiandao;

    @BindView(R.id.ac_prechreck_yujian2222)
    Button acYujian;
    private PreChereckActivity instances = null;
    private List<CarTypeModel> carTypeList = new ArrayList<>();
    private String Login_name = "", login_IDNumber;//当前登录的检测员的名字
    private String[] carTypeArraays = null;
    private String currentCode = "02";
    private boolean isNewYujian;
    TianJin22Model tianJin22Model=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pre_chereck);
        ButterKnife.bind(this);
        setBackBtn();
        setTopTitle("预  检");
        String logStrf = "\n" + "onCreate-----天津预检-------" + "\n";
        PDALogUtils.createLogFile(BaseApplication.JIANCE_MODE, logStrf.getBytes());
        instances = this;

        ZXingLibrary.initDisplayOpinion(this);
        Login_name = getIntent().getExtras().getString("login_UserID", "");
        login_IDNumber = getIntent().getExtras().getString("login_IDNumber", "");
        acPrechreckUser.setText(Login_name);
        isNewYujian = false;
        if(SharedPreferencesUtils.getTianJinVersion(this)){
            acYujian.setVisibility(View.VISIBLE);
        }else{
            acYujian.setVisibility(View.GONE);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        getData(0);
    }

    private void getData(int flag) {
        if (!TextUtils.isEmpty(SharedPreferencesUtils.getIP(instances))) {
            if (CommonUtils.isOnline(instances)) {
//                getDataReportData();
                switch (flag) {
                    case 0:
                        getCarTypeList();
                        break;
                    case 1:
//                        getDataWaiJainEnd();
                        break;
                }
            } else {
                Toast.makeText(instances, getResources().getString(R.string.network_error), Toast
                        .LENGTH_LONG).show();
            }
        } else {
            Toast.makeText(instances, "请设置IP与端口号", Toast.LENGTH_LONG).show();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        OkGo.getInstance().cancelTag(this);
        instances = null;
        String logStrf = "\n" + "onDestroy-----天津预检-------" + "\n";
        PDALogUtils.createLogFile(BaseApplication.JIANCE_MODE, logStrf.getBytes());
    }

    @OnClick({R.id.ac_prechreck_number, R.id.ac_prechreck_user,
            R.id.ac_prechreck_type, R.id.ac_prechreck_waijian_submit,
            R.id.ac_prechreck_waijian_query, R.id.ac_prechreck_category,
            R.id.ac_prechreck_order, R.id.ac_prechreck_new_yujian,
            R.id.ac_prechreck_yujian2222,R.id.ac_prechreck_qiandao_qiandao})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ac_prechreck_qiandao_qiandao:
                //签到
//                if(tianJin22Model!=null){
//                    tianjian_qiandao(tianJin22Model.getData().getCP(),tianJin22Model.getData().getID());
//                }
                if(!TextUtils.isEmpty(qrcode_result)){
                    tianjian_qiandao("","",qrcode_result);
                }else{
                    Toast.makeText(this,"请先扫码后签到",Toast.LENGTH_LONG).show();
                }
                break;
            case R.id.ac_prechreck_yujian2222:
                //扫码
//                Intent intent=new Intent(this,TianJingQRcodeActivity.class);
//                startActivity(intent);
                Intent intent = new Intent(this, CaptureActivity.class);
                startActivityForResult(intent, REQUEST_CODE);
                break;
            case R.id.ac_prechreck_number:
                break;
            case R.id.ac_prechreck_user:
                break;
            case R.id.ac_prechreck_type:
                if (carTypeArraays != null) {
                    showMyDialog(carTypeArraays, R.id.ac_prechreck_type);
                }
                break;
            case R.id.ac_prechreck_waijian_submit:
                String carNO = acPrechreckNumber.getText().toString().trim().toUpperCase();
                String carType = acPrechreckType.getText().toString().trim();
                if (!TextUtils.isEmpty(carNO)) {
                    UtilsLog.e("carNO==" + carNO);
                    uploadDataYuJian(carNO);
                } else {
                    Toast.makeText(instances, "车牌号码不能为空", Toast.LENGTH_LONG).show();
                }
                break;
            case R.id.ac_prechreck_order:
                String logStr = "-----预检---点击预约查询按钮----";
                PDALogUtils.createLogFile(BaseApplication.JIANCE_MODE, logStr.getBytes());
                String OrderDate = DateUtil.getTodayDate2();
//                String PlateType = "";
                String PlateType = (acPrechreckType.getText().toString().trim()).substring(0, 2);
                prechreckQuery(OrderDate, PlateType);
                break;
            case R.id.ac_prechreck_waijian_query:
                String logStr2 = "-----预检---点击获取车辆信息按钮----";
                PDALogUtils.createLogFile(BaseApplication.JIANCE_MODE, logStr2.getBytes());
                String plateType = (acPrechreckType.getText().toString().trim()).substring(0, 2);
                String PlateNO = acPrechreckNumber.getText().toString().trim();
                String VIN = acPrechreckVin.getText().toString().trim();
                String DetectionCategory = (acPrechreckCategory.getText().toString().trim()).substring(0, 2);
                if (!TextUtils.isEmpty(PlateNO) && !TextUtils.isEmpty(VIN)) {
                    SearchPlatformCar(PlateNO, VIN, plateType, DetectionCategory);
                } else {
                    Toast.makeText(instances, "请补全车牌号和VIN", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.ac_prechreck_category:
                showMyDialog(detectionCategoryArray, R.id.ac_prechreck_category);
                break;
            case R.id.ac_prechreck_new_yujian:
                isNewYujian = true;
                Toast.makeText(this, "新平台预检", Toast.LENGTH_LONG).show();
                break;
        }
    }

    private void SearchPlatformCar(String plateNo, String vin, String plateType, String detectionCategory) {
        SearchPlatformCarModel model = new SearchPlatformCarModel();
        model.setPlateNO(plateNo);
        model.setVIN(vin);
        model.setPlateType(plateType);
        model.setDetectionCategory(detectionCategory);
        Gson gson = new Gson();
        String jsonStr = gson.toJson(model);
        String logStr = "\n" + "预检--获取车辆信息----" + "\n" + "URL::" +
                SharedPreferencesUtils.getIP(instances) + SEARCH_PLATFORM_CAR + "\n" + "jsonStr===" + jsonStr;
        UtilsLog.e(logStr);
        PDALogUtils.createLogFile(BaseApplication.JIANCE_MODE, logStr.getBytes());
        OkGo.<String>post(SharedPreferencesUtils.getIP(instances) + SEARCH_PLATFORM_CAR).tag(instances)
                .upJson(jsonStr)
                .execute(new StringCallback() {
                    @Override
                    public void onStart(Request<String, ? extends Request> request) {
                        super.onStart(request);
                        CommonUtils.showLoadingDialog(instances, "查询中...");
                    }

                    @Override
                    public void onSuccess(Response<String> response) {
                        CommonUtils.hideLoadingDialog(instances);
                        String result = response.body();
                        String newResult = result.substring(1, result.length() - 1).replaceAll("\\\\", "");
                        UtilsLog.e("请求成功----result===" + result + "\n" + "newResult===" + newResult);
                        String logStr = "\n" + "预检---查询车辆信息----onSuccess---" + "\n" + "result::" + response.body() + "\n" + "newResult===" + newResult;
                        PDALogUtils.createLogFile(BaseApplication.JIANCE_MODE, logStr.getBytes());
                        if (!TextUtils.isEmpty(result)) {
                            if (result.contains("服务器端")) {
//                                Toast.makeText(instances, result, Toast.LENGTH_LONG).show();
                                showDialog(newResult);
                            }
                            try {
//                                JSONObject jsonObject = new JSONObject(newResult);
                                JSONArray jsonArray = new JSONArray(newResult);
                                PDALogUtils.createLogFile(BaseApplication.JIANCE_MODE, ("result--jsonArray====" + jsonArray.toString()).getBytes());
                                String VIN = jsonArray.getJSONObject(0).getString("VIN");
                                String VehicleStatus = jsonArray.getJSONObject(0).getString("VehicleStatus");
                                String CertificateNo = jsonArray.getJSONObject(0).getString("CertificateNo");
                                String RegisteDate = jsonArray.getJSONObject(0).getString("RegisteDate");
                                String ValidTill = jsonArray.getJSONObject(0).getString("ValidTill");
                                String ScrapDate = jsonArray.getJSONObject(0).getString("ScrapDate");
                                String Status = jsonArray.getJSONObject(0).getString("Status");
                                acPrechreckVin2.setText(VIN);
                                if (VehicleStatus.equals("正常") || VehicleStatus.equals("") || VehicleStatus.equals("A")) {
                                    acPrechreckVehicleStatus.setText("正常");
                                } else {
                                    acPrechreckVehicleStatus.setText(VehicleStatus);
                                }
                                acPrechreckCertificateNo.setText(CertificateNo);
                                acPrechreckRegisteDate.setText(RegisteDate);
                                acPrechreckValidTill.setText(ValidTill);
                                acPrechreckScrapDate.setText(ScrapDate);
                                if (Status.contains("免检")) {
                                    acPrechreckStatus.setText("免检");
                                } else {
                                    acPrechreckStatus.setText("不免检");
                                }

                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        } else {
                            Toast.makeText(instances, "此查询条件下无车辆信息数据", Toast.LENGTH_LONG).show();
                        }

                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        CommonUtils.hideLoadingDialog(instances);
                        String errorMessage = response.message();
                        UtilsLog.e("onErrorMessage===" + errorMessage);
                        String logStr = "---预检----查询车辆信息-----onError-----" + "\n" + "URL::" +
                                SharedPreferencesUtils.getIP(instances) + SEARCH_PLATFORM_CAR + "\n" +
                                "result::" + errorMessage;
                        PDALogUtils.createLogFile(BaseApplication.JIANCE_MODE, logStr.getBytes());
                        Toast.makeText(instances, "请求失败" + "\n" + "onErrorMessage===" + errorMessage, Toast.LENGTH_LONG).show();
                    }
                });
    }

    private void showDialog(String content) {
        AlertDialog.Builder builder = new AlertDialog.Builder
                (instances);
        builder.setTitle("提示");
        builder.setMessage(content);
//        builder.setNegativeButton("取消", null);
        builder.setPositiveButton("确定", null);
        builder.show();
    }

    private void prechreckQuery(String OrderDate, String PlateType) {
        String currentCarNO = acPrechreckNumber.getText().toString().trim();
//        String carNO = acPrechreckNumber.getText().toString().trim().toUpperCase();
        QueryModel queryModel = new QueryModel();
        queryModel.setOrderDate(OrderDate);
        queryModel.setPlateType(PlateType);
        queryModel.setStatus(99);
        queryModel.setCarNO(currentCarNO);
        queryModel.setIsNew("1");
        Gson gson = new Gson();
        String jsonStr = gson.toJson(queryModel);
        String logStr = "\n" + "预检--预约查询----" + "\n" + "URL::" +
                SharedPreferencesUtils.getIP(instances) + ORDER_INFO_DOWN + "\n" + "jsonStr===" + jsonStr;
        PDALogUtils.createLogFile(BaseApplication.JIANCE_MODE, logStr.getBytes());
        UtilsLog.e("预检--查询---url==" + SharedPreferencesUtils.getIP(instances) + ORDER_INFO_DOWN);
        OkGo.<String>post(SharedPreferencesUtils.getIP(instances) + ORDER_INFO_DOWN)
                .tag(instances)
                .upJson(jsonStr)
                .execute(new StringCallback() {
                    @Override
                    public void onStart(Request<String, ? extends Request> request) {
                        super.onStart(request);
                    }

                    @Override
                    public void onSuccess(Response<String> response) {
                        String result = response.body();
                        String newResult = result.substring(1, result.length() - 1).replaceAll("\\\\", "");
                        UtilsLog.e("请求成功----result===" + result + "\n" + "newResult===" + newResult);
                        String logStr = "\n" + "预检---预约查询----onSuccess---" + "\n" + "result::" + result + "\n"
                                + "newResult::" + newResult;
                        PDALogUtils.createLogFile(BaseApplication.JIANCE_MODE, logStr.getBytes());
//                        List<YujianOrderModel> yujianOrderModelList = new ArrayList<YujianOrderModel>();
//                        List<String> hphmList = new ArrayList<>();
                        if (!TextUtils.isEmpty(newResult) && !"[[]]".equals(newResult) && !"[{}]".equals
                                (newResult) && !"[]".equals(newResult)) {
//                            if (Utils.isJson(newResult)) {
//                                // 字符串是有效的 JSON
//                                List<T0Model> t0ModelList = new ArrayList<T0Model>();
//                                t0ModelList=JsonUtil.stringToArray(newResult,T0Model[].class);
//                                if (t0ModelList != null && !t0ModelList.isEmpty()) {
//                                    if(!TextUtils.isEmpty(t0ModelList.get(0).getResult())){
//                                        TipDialog.show(instances, "预约车辆", TipDialog.TYPE.SUCCESS);
//                                    } else {
//                                        TipDialog.show(instances, "非预约车辆", TipDialog.TYPE.SUCCESS);
//                                    }
//                                }
//                            } else {
//                                // 字符串不是有效的 JSON
//                                TipDialog.show(instances, newResult+" ", TipDialog.TYPE.ERROR);
//                            }


                            if ("ok".equalsIgnoreCase(newResult)) {
                                TipDialog.show(instances, "预约车辆", TipDialog.TYPE.SUCCESS);
                            } else {
                                TipDialog.show(instances, "非预约车辆", TipDialog.TYPE.SUCCESS);
                            }
//                            try {
//                                yujianOrderModelList = JsonUtil.stringToArray(newResult, YujianOrderModel[].class);
//                            } catch (Exception e) {
//                                String logStrsss = "\n" + "预检---预约查询----catch---" + "\n" + "e::" +
//                                        e.getMessage() + "\n";
//                                PDALogUtils.createLogFile(BaseApplication.JIANCE_MODE, logStrsss.getBytes());
//                            } finally {
//                                if (yujianOrderModelList != null && yujianOrderModelList.size() > 0) {
//                                    for (int i = 0; i < yujianOrderModelList.size(); i++) {
//                                        String hphm = yujianOrderModelList.get(i).getHphm();
//                                        hphmList.add(hphm);
//                                    }
//                                    if (hphmList.contains(currentCarNO)) {
//                                        TipDialog.show(instances, "预约车辆", TipDialog.TYPE.SUCCESS);
//                                        String logStr2 = "当前预约查询的车牌号码为===" + currentCarNO + "\n"
//                                                + "平台返回的预约成功车辆列表----hphmList===" + hphmList + "\n" + "result::预约车辆" + "\n";
//                                        PDALogUtils.createLogFile(BaseApplication.JIANCE_MODE, logStr2.getBytes());
//                                    } else {
//                                        TipDialog.show(instances, "非预约车辆", TipDialog.TYPE.SUCCESS);
//                                        String logStr2 = "当前预约查询的车牌号码为===" + currentCarNO + "\n"
//                                                + "平台返回的预约成功车辆列表----hphmList===" + hphmList + "\n" + "result::非预约车辆" + "\n";
//                                        PDALogUtils.createLogFile(BaseApplication.JIANCE_MODE, logStr2.getBytes());
//                                    }
//                                }
//                            }
                        }else {
                            TipDialog.show(instances, "非预约车辆", TipDialog.TYPE.SUCCESS);
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        String errorMessage = response.message();
                        UtilsLog.e("onErrorMessage===" + errorMessage);
                        String errorLog = "预约查询请求失败-----onError----result===" + errorMessage;
                        PDALogUtils.createLogFile(BaseApplication.JIANCE_MODE, errorLog.getBytes());
                        Toast.makeText(instances, "预约查询请求失败", Toast.LENGTH_LONG).show();
                    }
                });
    }

    private void getCarTypeList() {
        String url = CAR_TYPES;
        String logStr = "\n" + "获取所有车辆类型----" + "\n" + "URL::" +
                SharedPreferencesUtils.getIP(instances) + url + "\n";
        PDALogUtils.createLogFile(BaseApplication.JIANCE_MODE, logStr.getBytes());
        UtilsLog.e("getGET_ALL_ITEMList---url==" + SharedPreferencesUtils.getIP(instances) + url);
        OkGo.<String>get(SharedPreferencesUtils.getIP(instances) + url).tag(instances)
                .execute(new StringCallback() {
                    @Override
                    public void onStart(Request<String, ? extends Request> request) {
                        super.onStart(request);
                        CommonUtils.showLoadingDialog(instances, "加载中...");
                    }

                    @Override
                    public void onSuccess(Response<String> response) {
                        CommonUtils.hideLoadingDialog(instances);
                        String result = response.body();
                        String newResult = result.substring(1, result.length() - 1).replaceAll("\\\\", "");
                        UtilsLog.e("getCarTypeList-newResult==" + newResult);
                        String logStr = "\n" + "获取所有车辆类型----onSuccess" + "\n" + "result::" + response.body() + "\n";
                        PDALogUtils.createLogFile(BaseApplication.JIANCE_MODE, logStr.getBytes());
                        if (!TextUtils.isEmpty(newResult) && !"[[]]".equals(newResult) && !"[{}]".equals
                                (newResult) && !"[]".equals(result)) {
                            carTypeList = new ArrayList<CarTypeModel>();
                            carTypeList = JsonUtil.stringToArray(newResult, CarTypeModel[].class);
                            if (carTypeList != null && carTypeList.size() > 0) {
                                carTypeArraays = new String[carTypeList.size()];
                                for (int i = 0; i < carTypeList.size(); i++) {
                                    carTypeArraays[i] = carTypeList.get(i).getName();
                                }
                                UtilsLog.e("carTypeArraays==" + carTypeArraays.toString());
                            } else {
                                Toast.makeText(instances, "获取所有车辆类型失败，请重试",
                                        Toast.LENGTH_LONG).show();
                            }
                        } else {
                            Toast.makeText(instances, "获取车辆类型失败", Toast.LENGTH_LONG).show();
                            UtilsLog.e("getGET_ALL_ITEMList-result==" + "没有数据");
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        CommonUtils.hideLoadingDialog(instances);
                        UtilsLog.e("getGET_ALL_ITEMList-onError==" + response.body());
                        String logStr = "\n" + "获取所有车辆类型----onError" + "\n" + "result::" + response.body() + "\n";
                        PDALogUtils.createLogFile(BaseApplication.JIANCE_MODE, logStr.getBytes());
                        Toast.makeText(instances, getResources().getString(R.string.services_error), Toast
                                .LENGTH_LONG).show();
                    }
                });
    }

    //天津预检数据上传
    private void uploadDataYuJian(String carNO) {
        Model model = new Model();
        model.setPlateNO(carNO);
        model.setAppearanceInspector(Login_name);
        model.setPlateType(currentCode);
        model.setStatus("1");
        if (isNewYujian) {
            model.setCarNO(carNO);
            model.setIsNew("1");
        }
        Gson gson = new Gson();
        String jsonStr = gson.toJson(model);

//        String upjson_url = "{[" + jsonStr.substring(1, jsonStr.length() - 1) + "]}";
        String upjson_url = "{" + jsonStr.substring(1, jsonStr.length() - 1) + "}";

        String logStr = "\n" + "天津预检数据上传----json" + "\n" +
                "json::" + upjson_url + "\n" +
                "URL::" + SharedPreferencesUtils.getIP(instances) + YU_JIAN;
        PDALogUtils.createLogFile(BaseApplication.JIANCE_MODE, logStr.getBytes());
        UtilsLog.e("天津预检数据上传---jsonStr==" + upjson_url);
        UtilsLog.e("天津预检数据上传---url==" + SharedPreferencesUtils.getIP(this) + YU_JIAN);
        OkGo.<String>post(SharedPreferencesUtils.getIP(this) + YU_JIAN)
                .tag(this)
                .upJson(upjson_url)
                .execute(new StringCallback() {
                    @Override
                    public void onStart(Request<String, ? extends Request> request) {
                        super.onStart(request);
                        CommonUtils.showLoadingDialog(instances, "加载中...");
                    }

                    @Override
                    public void onSuccess(Response<String> response) {
                        String result = response.body().trim();
                        UtilsLog.e("天津预检数据上传-result==" + result);
                        UtilsLog.e("天津预检数据上传-result=22=" + result.substring(1, result.length() -
                                1));
                        String logStr = "\n" + "天津预检数据上传----请求成功" + "\n" +
                                "result::" + response.body() + "\n" +
                                "URL::" + SharedPreferencesUtils.getIP(instances) + YU_JIAN;
                        PDALogUtils.createLogFile(BaseApplication.JIANCE_MODE, logStr.getBytes());
                        CommonUtils.hideLoadingDialog(instances);
                        String str = result.substring(1, result.length() - 1);
                        if (str.contains("操作成功") || "ok".equalsIgnoreCase(str)) {
//                        if ("ok".equalsIgnoreCase(str)) {
                            Toast.makeText(instances, "提交数据成功", Toast.LENGTH_LONG)
                                    .show();
                            finish();
                        } else {
                            Toast.makeText(instances, "提交数据失败，请重试", Toast.LENGTH_LONG)
                                    .show();
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        String logStr2 = "\n" + "天津预检数据上传--onError" + "\n" +
                                "result::" + response.body();
                        PDALogUtils.createLogFile(BaseApplication.JIANCE_MODE, logStr2.getBytes());
                        UtilsLog.e("天津预检数据上传-onError==" + response.body());
                        CommonUtils.hideLoadingDialog(instances);
                    }
                });
    }

    private String[] detectionCategoryArray = {"00-注册登记安全检验", "01-在用机动车安全检验(定检)", "02-临时检验", "03-特殊检验", "04-在用机动车安全检验(非定检)", "99-其他检测"};

    private void showMyDialog(String[] str, final int id) {
//        BottomMenu bottomMenu= BottomMenu().build(this);
        BottomMenu.show(this, str, new OnMenuItemClickListener() {
            @Override
            public void onClick(String text, int index) {
                //返回参数 text 即菜单名称，index 即菜单索引
                UtilsLog.e("---showMyDialog==" + text + "===" + index);
                switch (id) {
                    case R.id.ac_prechreck_type:
                        acPrechreckType.setText(text);
                        currentCode = carTypeList.get(index).getCode();
                        UtilsLog.e("---showMyDialog==currentCode===" + currentCode);
                        break;
                    case R.id.ac_prechreck_category:
                        acPrechreckCategory.setText(text);
                        break;
                }
            }
        }).setMenuTextInfo(new TextInfo().setBold(true).setFontColor(Color.BLACK).setGravity(Gravity.CENTER));
    }

    class Model {
        private String PlateType, PlateNO, AppearanceInspector, IsNew, CarNO, Status;

        public String getStatus() {
            return Status;
        }

        public void setStatus(String status) {
            Status = status;
        }

        @Override
        public String toString() {
            return "Model{" +
                    "PlateType='" + PlateType + '\'' +
                    ", PlateNO='" + PlateNO + '\'' +
                    ", AppearanceInspector='" + AppearanceInspector + '\'' +
                    ", IsNew='" + IsNew + '\'' +
                    ", CarNO='" + CarNO + '\'' +
                    ", Status='" + Status + '\'' +
                    '}';
        }

        public String getIsNew() {
            return IsNew;
        }

        public void setIsNew(String isNew) {
            IsNew = isNew;
        }

        public String getCarNO() {
            return CarNO;
        }

        public void setCarNO(String carNO) {
            CarNO = carNO;
        }

        public String getPlateType() {
            return PlateType;
        }

        public void setPlateType(String plateType) {
            PlateType = plateType;
        }

        public String getPlateNO() {
            return PlateNO;
        }

        public void setPlateNO(String plateNO) {
            PlateNO = plateNO;
        }

        public String getAppearanceInspector() {
            return AppearanceInspector;
        }

        public void setAppearanceInspector(String appearanceInspector) {
            AppearanceInspector = appearanceInspector;
        }
    }



    @Override
// 通过 onActivityResult的方法获取 扫描回来的 值
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        String logStrf = "\n" + "onCreate-----扫码签到---" + "\n";
        /**
         * 处理二维码扫描结果
         */
        if (requestCode == REQUEST_CODE) {
            //处理扫描结果（在界面上显示）
            if (null != data) {
                Bundle bundle = data.getExtras();
                if (bundle == null) {
                    return;
                }
                if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_SUCCESS) {
                    String result = bundle.getString(CodeUtils.RESULT_STRING);
                    if (!TextUtils.isEmpty(result)) {
                        tianjian_qrcode(result);
                    }
//                        Toast.makeText(this, "解析结果:" + result, Toast.LENGTH_LONG).show();
                    UtilsLog.e("扫描结果：" + result);
                    logStrf = logStrf + "扫描结果:" + result + "\n";
                } else if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_FAILED) {
                    Toast.makeText(instances, "解析二维码失败", Toast.LENGTH_LONG).show();
                    logStrf = logStrf + "解析二维码失败" + "\n";
                }
                PDALogUtils.createLogFile(BaseApplication.JIANCE_MODE, logStrf.getBytes());
            }
        }
    }

    String qrcode_result="";
    private void tianjian_qrcode(String code) {
        QRCode model = new QRCode();
        model.setMsg(code);
//        String jsonStr="{\"msg:\""+code+"\"}";
        Gson gson = new Gson();
        String jsonStr = gson.toJson(model);
        String logStr = "\n" + "天津扫码-----json" + "\n" + "json::" + jsonStr + "\n" +
                "URL::" + SharedPreferencesUtils.getIP(instances) + TIANJIN_QRCODE;
        PDALogUtils.createLogFile(BaseApplication.JIANCE_MODE, logStr.getBytes());
        UtilsLog.e("天津扫码----jsonStr==" + jsonStr);
        UtilsLog.e("天津扫码----url==" + SharedPreferencesUtils.getIP(this) + TIANJIN_QRCODE);
        OkGo.<String>post(SharedPreferencesUtils.getIP(this) + TIANJIN_QRCODE)
                .tag(this)
                .upJson(jsonStr)
                .execute(new StringCallback() {
                    @Override
                    public void onStart(Request<String, ? extends Request> request) {
                        super.onStart(request);
//                        CommonUtils.showLoadingDialog(instances, "加载中...");
                        WaitDialog.show(instances, "请稍候...");
                    }

                    @Override
                    public void onSuccess(Response<String> response) {
                        WaitDialog.dismiss();
                        String result = response.body().trim();
                        UtilsLog.e("天津扫码----result==" + result);
//                        UtilsLog.e("天津扫码----result=22=" + result.substring(1, result.length() -
//                                1));
//                        String newResult = result.substring(1, result.length() - 1).replaceAll("\\\\", "");
                        String logStr = "\n" + "天津扫码---请求成功" + "\n" + "result::" + result + "\n";
                        PDALogUtils.createLogFile(BaseApplication.JIANCE_MODE, logStr.getBytes());
//                        CommonUtils.hideLoadingDialog(instances);
                        try {
                            JSONObject jsonObject = new JSONObject(result);
                            int code = jsonObject.getInt("code");
                            if (code == 200) {
                                qrcode_result=result;
                                tianJin22Model = JsonUtil.fromJson(result, TianJin22Model.class);
                                if (tianJin22Model != null) {
                                    acPrechreckNumber.setText(tianJin22Model.getData().getCP());
//                                    acQrcodeId.setText(tianJin22Model.getData().getID());
//                                    acQrcodeHaopaiType.setText(tianJin22Model.getData().getHP());
                                    acPrechreckVin.setText(tianJin22Model.getData().getCJ());
//                                    acPrechreckVin2.setText(tianJin22Model.getData().getCJ());
//                                    acQrcode.setText(tianJin22Model.getData().getRQ() + "  " + tianJin22Model.getData().getSJ());
//                                    acQrcodeJiancezhanCode.setText(tianJin22Model.getData().getJG());
                                }
                            } else {
                                Toast.makeText(instances, jsonObject.getString("msg"), Toast.LENGTH_LONG).show();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
//                        if ("ok".equalsIgnoreCase(str)) {
//                            TipDialog.show(instances, "请求成功", TipDialog.TYPE.SUCCESS);
//                        } else {
//                            MessageDialog.show(instances, "提示", "指标核查请求失败，请重试", "确定");
//                        }

                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        WaitDialog.dismiss();
                        String logStr2 = "\n" + "天津扫码----onError" + "\n" +
                                "result::" + response.body();
                        PDALogUtils.createLogFile(BaseApplication.JIANCE_MODE, logStr2.getBytes());
                        UtilsLog.e("天津扫码----onError==" + response.body());
//                        CommonUtils.hideLoadingDialog(instances);
                        MessageDialog.show(instances, "提示", "扫描二维码异常，请重试", "确定");
                    }
                });
    }


    private void tianjian_qiandao(String v1,String v2,String result) {
//        QianDao model=new QianDao();
//        model.setHphm(v1);
//        model.setOrder_id(v2);
//        String jsonStr="{\"msg\":"+code+"}";
        Gson gson = new Gson();
        String jsonStr = gson.toJson(result);
        String logStr = "\n" + "天津签到-----json" + "\n" + "json::" + jsonStr + "\n" +
                "URL::" + SharedPreferencesUtils.getIP(instances) + TIANJIAN_QIANDAO;
        PDALogUtils.createLogFile(BaseApplication.JIANCE_MODE, logStr.getBytes());
        UtilsLog.e("天津签到----jsonStr==" + jsonStr);
        UtilsLog.e("天津签到----url==" + SharedPreferencesUtils.getIP(this) + TIANJIAN_QIANDAO);
        OkGo.<String>post(SharedPreferencesUtils.getIP(this) + TIANJIAN_QIANDAO)
                .tag(this)
                .upJson(jsonStr)
                .execute(new StringCallback() {
                    @Override
                    public void onStart(Request<String, ? extends Request> request) {
                        super.onStart(request);
//                        CommonUtils.showLoadingDialog(instances, "加载中...");
                        WaitDialog.show(instances, "请稍候...");
                    }

                    @Override
                    public void onSuccess(Response<String> response) {
                        WaitDialog.dismiss();
                        String result = response.body().trim();
                        UtilsLog.e("天津签到----result==" + result);
//                        UtilsLog.e("天津签到----result=22=" + result.substring(1, result.length() -
//                                1));
//                        String newResult = result.substring(1, result.length() - 1).replaceAll("\\\\", "");
                        String logStr = "\n" + "天津签到------请求成功" + "\n" + "result::" +result + "\n" ;
                        PDALogUtils.createLogFile(BaseApplication.JIANCE_MODE, logStr.getBytes());
//                        CommonUtils.hideLoadingDialog(instances);
//                        try {
//                            JSONArray jsonArray = new JSONArray(newResult);
//                            String tip = jsonArray.getJSONObject(0).getJSONObject("result").getString("hcsm");
//                            Toast.makeText(instances, tip, Toast.LENGTH_LONG).show();
//                        } catch (JSONException e) {
//                            e.printStackTrace();
//                        }
                        if ("ok".equalsIgnoreCase(result)) {
                            TipDialog.show(instances, "成功", TipDialog.TYPE.SUCCESS);
                        } else {
                            MessageDialog.show(instances, "提示", result, "确定");
                        }

                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        WaitDialog.dismiss();
                        String logStr2 = "\n" + "天津签到----onError" + "\n" +
                                "result::" + response.body();
                        PDALogUtils.createLogFile(BaseApplication.JIANCE_MODE, logStr2.getBytes());
                        UtilsLog.e("天津签到----onError==" + response.body());
//                        CommonUtils.hideLoadingDialog(instances);
                        MessageDialog.show(instances, "提示", "扫描二维码异常，请重试", "确定");
                    }
                });
    }

    class QianDao{
        String hphm,order_id;

        public String getHphm() {
            return hphm;
        }

        public void setHphm(String hphm) {
            this.hphm = hphm;
        }

        public String getOrder_id() {
            return order_id;
        }

        public void setOrder_id(String order_id) {
            this.order_id = order_id;
        }

        @Override
        public String toString() {
            return "QianDao{" +
                    "hphm='" + hphm + '\'' +
                    ", order_id='" + order_id + '\'' +
                    '}';
        }
    }
    class QRCode{
        String msg;

        public String getMsg() {
            return msg;
        }

        public void setMsg(String msg) {
            this.msg = msg;
        }

        @Override
        public String toString() {
            return "QRCode{" +
                    "msg='" + msg + '\'' +
                    '}';
        }
    }
}
